//
//  GMSystemTool.m
//  News
//
//  Created by 雷国敏 on 16/9/20.
//  Copyright © 2016年 lieop. All rights reserved.
//

#import "GMSystemTool.h"
#import "AFNetworkReachabilityManager.h"
#import "LYNONetworkView.h"

@implementation GMSystemTool

#pragma mark 清除缓存
/**
 *  计算整个目录大小
 */
+(float)folderSizeAtPath
{
	NSString *folderPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
	
	NSFileManager * manager=[NSFileManager defaultManager ];
	if (![manager fileExistsAtPath :folderPath]) {
		return 0 ;
	}
	NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
	NSString * fileName;
	long long folderSize = 0 ;
	while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
		NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
		folderSize += [ self fileSizeAtPath :fileAbsolutePath];
	}
	
	return folderSize/( 1024.0 * 1024.0 );
}



/**
 *  计算单个文件大小
 */
+(long long)fileSizeAtPath:(NSString *)filePath{
	
	NSFileManager *manager = [NSFileManager defaultManager];
	
	if ([manager fileExistsAtPath :filePath]){
		
		return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize];
	}
	return 0 ;
	
}



/**
 *  清理缓存
 */
+(void)cleanCache:(cleanCacheBlock)block
{
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
		//文件路径
		NSString *directoryPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
		
		NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
		
		for (NSString *subPath in subpaths) {
			NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
			[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
		}
		//返回主线程
		dispatch_async(dispatch_get_main_queue(), ^{
			block();
		});
	});
	
}


+(void)gm_reachability{
	[[AFNetworkReachabilityManager sharedManager] startMonitoring];
	[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
		if (status <= 0) {
			[[[LYNONetworkView alloc]init] gm_showNoNetwork];
		}
		
	}];
}

@end
